<?php
// +-------------------------------------------------------------+
// | Tinysupe [ SIMPLE IS THE BEST ! ]
// +-------------------------------------------------------------+
// | Copyright (c) 2011 www.srccn.com All rights reserved.
// +-------------------------------------------------------------+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +-------------------------------------------------------------+
// | Author: tinysu <tinysu@qq.com>
// +-------------------------------------------------------------+

class xmldb{
	protected $xml;

	function __construct($xmldb=null){
		if(!is_null($xmldb)) $this->open($xmldb);
	}
	
	function open($xmldb){
		$this->xml = @simplexml_load_file($xmldb);
		if(!$this->xml) die($xmldb.' error!');
	}
	
	public function getxml(){
		return $this->xml;
	}
	
	public function toarray(){
		return $this->xml2array($this->xml);
	}
	
	public function xpath($path){
		return $this->xml->xpath($path);
	}
	
	private function xml2array($xml){
		if(!is_string($xml)){
			if(empty($xml)){
				return (string)$xml;
			}
			$ret = array();
			foreach((array)$xml as $key => $val){
				$ret[$key] = $this->xml2array($val);
			}
			return $ret;
		}
		return (string)$xml;
	}
	
	function __destruct(){
		if($this->xml) unset($this->xml);	
	}
}
?>
